/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package generatorGraphics.interfaces;

import generatorGraphics.graphicsCategory.CategoryInterface;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

/**
 *
 * @author Heitor
 */
public class JPCategory extends JPanel {
    
    private ArrayList<CategoryInterface> categoryList;
    
    private CategoryInterface catActive;
    private Integer catIdActive;
    
    /** 
     * Método extra para geração de icones (Em analise)
     */
    private static final GridBagConstraints gbc;  
    static {  
        gbc = new GridBagConstraints();  
        gbc.gridx = 0;  
        gbc.gridy = 0;  
        gbc.weightx = 1.0;  
        gbc.weighty = 1.0;  
        gbc.fill = GridBagConstraints.BOTH;  
        gbc.anchor = GridBagConstraints.NORTHWEST;  
    }

    /**
     * Construtor
     */
    public JPCategory(){
        this.categoryList = new ArrayList<>();
                
        initComponents();        
        
        setVisible(true);
    }
    
    public JPCategory(ArrayList<CategoryInterface> categoryList){

        this.categoryList = categoryList;
        
        initComponents();        
        
        setVisible(true);
    }
    
    public JPCategory(int width, int height){    

        initComponents();
        
        setSize(width, height);
        setVisible(true);
    }

    /**
     * Inicia componentes gráficos
     */
    private void initComponents(){
   
        setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
        
        JButton jbutton_aux;
        ImageIcon image_aux;
        String name_aux;
            
        for(int i=0; i<categoryList.size(); i++){
        
            final int idCat = categoryList.get(i).getId();
            
            image_aux = categoryList.get(i).getImage();
            name_aux = categoryList.get(i).getName();
            
            jbutton_aux = new JButton();
            jbutton_aux.setText(name_aux);
            jbutton_aux.setIcon(image_aux);
            jbutton_aux.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e){
                    jbuttonActionPerformed(idCat);
                }
            });
            //jbutton_aux.setMaximumSize();
            jbutton_aux.setVisible(true);
            
            add(jbutton_aux);
        }

        setVisible(true);
        setBorder(null);

    }
    
    /**
     * Método de ação do botão
     */
    private void jbuttonActionPerformed(int idCat){
        
        this.catActive = getCategory(idCat);
        this.catIdActive = this.catActive.getId();
    }

    /**
     * @return the catIdActive
     */
    public Integer getCatIdActive() {
        return catIdActive;
    }

    /**
     * @return the catActive
     */
    public CategoryInterface getCatActive() {
        return catActive;
    }
    
    public CategoryInterface getCategory(int catId){
        CategoryInterface auxReturn = null;
        
        for(int f=0; f<categoryList.size(); f++){
            if (categoryList.get(f).getId() == catId){
                auxReturn = categoryList.get(f);
            }
        }
        
        return auxReturn;
    }
    
    public void addCategory(CategoryInterface category){
        boolean thereIs = false;
        
        for(int r=0; r<categoryList.size(); r++){
            if(categoryList.get(r).getName().equals(category.getName())){
                thereIs = true;
            }
        }
        
        if(!thereIs) {
            categoryList.add(category);
        }
        
        update();
    }
    
    private void update(){
        
        JButton jbutton_aux;
        ImageIcon image_aux;
        String name_aux;
        
        removeAll();
            
        for(int i=0; i<categoryList.size(); i++){            

            if(catActive == null){
                catActive = categoryList.get(i);
                catIdActive = categoryList.get(i).getId();
            }
        
            final int idCat = categoryList.get(i).getId();
            
            image_aux = categoryList.get(i).getImage();
            name_aux = categoryList.get(i).getName();
            
            jbutton_aux = new JButton();
            jbutton_aux.setText(name_aux);
            jbutton_aux.setIcon(image_aux);
            jbutton_aux.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e){
                    jbuttonActionPerformed(idCat);
                }
            });
            //jbutton_aux.setMaximumSize();
            jbutton_aux.setVisible(true);
            
            add(jbutton_aux);
        }
        
    }
    
}
